#!/usr/bin/python
#coding:utf-8

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import csvout
import sys

# driver = webdriver.Firefox()
driver = webdriver.PhantomJS()
# driver = webdriver.PhantomJS(executable_path="/home/gtmer/spider/phantomjs-2.1.1-linux-x86_64/bin/phantomjs")
url = 'https://www.oschina.net' + '/search?scope=blog&fromerr=HV5lAcBj&q=' + sys.argv[1]
driver.get(url)

time.sleep(5)
try:
    res = driver.find_element_by_xpath('//*[@id="ResultStats"]/font[1]').text
except:
    #如果找不到就报错，此时数值为0
    num = 0
else:
    num = filter(lambda x: x.isdigit(), res)
finally:
    outdic = {
        'oschina_num': num
    }
    outli = ['oschina_num', num, driver.current_url]
    csvout.list2csv(outli)
    print(outli)
    driver.close()